﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Linq;
public partial class Default2 : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GridView1.RowDataBound += new GridViewRowEventHandler(GridView1_RowDataBound);
            GridView1.DataBound += new EventHandler(GridView_DataBound);
            GridView1DataBind();


        }
    }
    void GridView1DataBind()
    {
        DataTable table = new DataTable();
        table.Columns.Add("class", typeof(string)).SetOrdinal(0);
        table.Columns.Add("student", typeof(string)).SetOrdinal(1);
        table.Columns.Add("Languages", typeof(Double)).SetOrdinal(2);
        table.Columns.Add("Rate1", typeof(Double)).SetOrdinal(3);
        table.Columns.Add("Mathematics", typeof(Double)).SetOrdinal(4);
        table.Columns.Add("Rate2", typeof(Double)).SetOrdinal(5);
        table.Columns.Add("English", typeof(Double)).SetOrdinal(6);
        table.Columns.Add("Rate3", typeof(Double)).SetOrdinal(7);
        table.Columns.Add("Other", typeof(Double)).SetOrdinal(8);
        table.Columns.Add("TotalScore", typeof(Double), "ISNULL(Languages,0)+ISNULL(Mathematics,0)+ISNULL(English,0)+ISNULL(Other,0)").SetOrdinal(9);

        table.Columns[3].Expression = "Languages/TotalScore";
        table.Columns[5].Expression ="Mathematics/TotalScore";
        table.Columns[7].Expression = "English/TotalScore";
        Random random = new Random();
        for (int i = 0; i < 100; i++)
        {
            DataRow row = table.NewRow();
            row["student"] = "学生" + i;
            row["class"] = "班级" + random.Next(0, 10);
            row["Languages"] = random.Next(0, 151);
            row["Mathematics"] = random.Next(0, 151);
            row["English"] = random.Next(0, 151);
           row["Other"] = random.Next(0, 150*3+1);
            table.Rows.Add(row);
        }
        
      //GridView1.DataSource =GridViewGroup.AddGurop(table, "class"); 
        GridView1.DataSource = GridViewGroup.AddGurop(table, "class", "Languages#Mathematics#English#Other"); 
        GridView1.DataBind();

    }
    void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            string header = "班级#学生#其中 语文 成绩,比例#其中 数学 成绩,比例#其中 英语 成绩,比例#其中 &nbsp; 其他3科#总成绩";
            AndyGridViewTHeaderHepler help = new AndyGridViewTHeaderHepler();
            e.Row.CssClass = "fixedHeaderTr";

            e.Row.BackColor = System.Drawing.Color.Beige;
            help.SplitTableHeader(e.Row, header); 
        }
        else
        {
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='White'");
            e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor=\"" + "#E7E7FF" + "\"");

        }
    }

    protected void GridView_DataBound(object sender, EventArgs e)
    {
        int index = -1;
        int startGroupIndex = 0;
        foreach (GridViewRow row in GridView1.Rows)
        {
            index++;

            if (index > 0 && (row.Cells[0].Text.ToString().Trim() == "" || row.Cells[0].Text.ToString().Trim() == "&nbsp;"))
            {
                row.Cells[0].Text = "小计";// GridView1.Rows[startGroupIndex].Cells[0].Text;
                row.Attributes.Add("style", "background-color: #F2F2F2; font-weight: bold;font-variant: normal; color: #000000;text-align:center;");
                row.Attributes.Remove("onmouseout");
                row.Attributes.Remove("onmouseover");
                GridViewGroup.GroupRow(GridView1, index, 0, 2);
                GridViewGroup.GropCell(GridView1, 0, startGroupIndex, index - 1);
                startGroupIndex = index + 1;
            }
        }
        GridViewRow sumRow = GridView1.Rows[index];
        sumRow.Attributes.Add("style", "background-color: #EAEAEA;font-weight: bold;font-variant: normal;color: #000000;text-align: center;");
        sumRow.Attributes.Remove("onmouseout");
        sumRow.Attributes.Remove("onmouseover");
        GridViewGroup.GroupRow(GridView1, index, 0, 2);
    }
}
